package com.example.algorithm.no0504.solution;

import com.example.algorithm.no0504.Solution;

/**
 * @author yumuhui <yumuhui@kuaishou.com>
 * Created on 2021-04-22
 * @Description 七进制数
 */
public class SolutionImpl implements Solution {

    @Override
    public String convertToBase7(int num) {
        if (num == 0) {
            return "";
        }

        // 是否为负数
        boolean b = false;
        if (num < 0) {
            b = true;
            num = - num;
        }

        StringBuffer sb = new StringBuffer();

        while (num > 0) {
            sb.append(num % 7);
            num /= 7;
        }

        return b ? "-" + sb.reverse().toString() : sb.reverse().toString();
    }
}
